
import java.io.*;
import java.security.*;
import javax.net.ssl.*;

public class Client implements Runnable {
	// Connection to the client
	private BufferedReader in;

	// Connection to the client
	private PrintWriter out;

	// KeyStore for storing our public/private key pair
	private KeyStore clientKeyStore;
  
	// KeyStore for storing the server's public key
	private KeyStore serverKeyStore;

	// Used to generate a SocketFactory
	private SSLContext sslContext;
	
	static XMLBuild xmlClient;
	String imeServerja;

	// A source of secure random numbers
	static private SecureRandom secureRandom;

	private String myJksFile, myPwd, otherJksFile,  otherPwd;
  	
	public Client(String host, int port , String myJksFile, String myPwd, String otherJksFile, String otherPwd ) {
		this.myJksFile = myJksFile;
		this.myPwd = myPwd;
		this.otherJksFile = otherJksFile;
		this.otherPwd = otherPwd;
		connect( host, port );
		new Thread( this ).start();
	}

	private void setupServerKeystore() throws GeneralSecurityException, IOException {
		serverKeyStore = KeyStore.getInstance( "JKS" );
		serverKeyStore.load( new FileInputStream(otherJksFile), otherPwd.toCharArray() );
	}

	private void setupClientKeyStore() throws GeneralSecurityException, IOException {
		clientKeyStore = KeyStore.getInstance( "JKS" );
		clientKeyStore.load( new FileInputStream( myJksFile ), myPwd.toCharArray() );
	}

	private void setupSSLContext() throws GeneralSecurityException, IOException {
		TrustManagerFactory tmf = TrustManagerFactory.getInstance( "SunX509" );
		tmf.init( serverKeyStore );

		KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" );
		kmf.init( clientKeyStore, myPwd.toCharArray() );

		sslContext = SSLContext.getInstance( "TLS" );
		sslContext.init( kmf.getKeyManagers(), tmf.getTrustManagers(), secureRandom );
	}

	private void connect( String host, int port ) {
		
		try {
			setupServerKeystore();
			setupClientKeyStore();
			setupSSLContext();

			SSLSocketFactory sf = sslContext.getSocketFactory();
			SSLSocket socket = (SSLSocket)sf.createSocket( host, port );
			String[] cs ={"TLS_RSA_WITH_AES_128_CBC_SHA"};
			socket.setEnabledCipherSuites(cs);
			imeServerja=((SSLSocket)socket).getSession().getPeerPrincipal().getName();
			out = new PrintWriter(socket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		} 
		catch( GeneralSecurityException gse ) {
			gse.printStackTrace();
		} 
		catch( IOException ie ) {
			ie.printStackTrace();
		}
	}

	public void run() {
		
		try{
			BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
			String fromUser;
			
			xmlClient = new XMLBuild(imeServerja);
			System.out.println("Nit je zagnana!");
			String s = "\n----------Glavni--Menu----------\n";
			s += "Kreiranje racuna..............k\n";
			s += "Polog........p [idRacuna][cena]\n";
			s += "Dvig.........d [idRacuna][cena]\n";
			s += "Izpis vseh racunov............i\n";
			s += "Izhod.........................q\n";
			
			String s2 = "Vnesite naziv racuna:\n";
			s2 += "Navadni...............n\n";
			s2 += "Studentski............s\n";
			s2 += "Upokojenski...........u\n";
			s2 += "Zlati.................z\n";
			
			System.out.println(in.readLine());
			while (true){
				
				System.out.println(s);
				fromUser = stdIn.readLine();
				
				if(fromUser.equals("k")){
					System.out.println(s2);
					fromUser = stdIn.readLine();

					if(fromUser.equals("n") || fromUser.equals("s") || fromUser.equals("u") || fromUser.equals("z") ){
						out.println(fromUser);
						System.out.println(in.readLine());
					}
					else
						System.out.println("Napacen ukaz!");
				}
				else if(fromUser.split(" ")[0].equals("p") || fromUser.split(" ")[0].equals("d")){
					try{
						Integer.parseInt(fromUser.split(" ")[1]);
						Integer.parseInt(fromUser.split(" ")[2]);
						out.println(fromUser);
						System.out.println(in.readLine());
					}catch (Exception e) {
						System.out.println("Napacen format ukaza: p [idRacuna][cena]");
					}
					
				}
				else if(fromUser.equals("i")){
					out.println(fromUser);
					System.out.println(in.readLine().replace(":", "\n"));
				}
				else if(fromUser.equals("q")){
					System.out.println("Povezava prekinjena!");
					break;
				}
				else
					System.out.println("Napacen ukaz!");
			}
		} 
		catch( IOException ie ){
			ie.printStackTrace();
		}
		System.out.println("Nit ubita!");
		
	} 
  
	static public void main( String args[] ){
		
	  if (args.length != 6){
		  System.err.println( "Usage: java Client [hostname] [port number]" );
		  System.exit( 1 );
	  }

	  String host = args[0];
	  int port = Integer.parseInt( args[1] );

	  System.out.println( "Wait while secure random numbers are initialized...." );
	  secureRandom = new SecureRandom();
	  secureRandom.nextInt();
	  System.out.println( "Done." );

	  new Client( host, port ,args[2],args[3],args[4],args[5]);
	}
}
